package com.mybank.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybank.pojo.Account;
import com.mybank.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @PostMapping
    public void save(@RequestBody Account account){
        accountService.save(account);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id){
        accountService.removeById(id);
    }
    @PutMapping
    public void update(@RequestBody Account account){
        accountService.updateById(account);
    }
    @GetMapping("/{id}")
    public Account getById(@PathVariable Integer id){
        return accountService.getById(id);
    }
    @GetMapping
    public List<Account> getAll(){
        return accountService.list();
    }
    @PostMapping("/page")
    public Page getByPage(@RequestBody Page page){
        return accountService.page(page);
    }

    /**
     * 登录验证
     * @param account 账户实体
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestBody Account account){
        return accountService.login(account);
    }

    /**
     * 转账操作
     * @return
     */
    @PostMapping("/transfer")
    public String transfer(@RequestBody HashMap<String,String> formData){
        return accountService.transfer(formData);
    }

    /**
     * 查询余额
     * @param cardno 卡号
     * @return 余额
     */
    @GetMapping("/balance/{cardno}")
    public Double getBalance(@PathVariable String cardno){
        return accountService.getBalance(cardno);
    }

    /**
     * 按卡号修改密码
     * @param params 入参
     * @return
     */
    @PostMapping("/updatePassword")
    public Integer updatePassword(@RequestBody HashMap<String,String> params){
        return accountService.updatePassword(params);
    }
}
